package com.goodving.guli.bt;

import android.app.ActivityManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Parcelable;
import android.os.PowerManager;

import com.goodving.guli.bt.model.HwBatteryStatus;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.profile.scale.C0638c;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.C1724A;
import com.goodving.guli.bt.profile.scale.C1798h;
import com.goodving.guli.bt.profile.scale.C3863r;
import com.goodving.guli.bt.profile.scale.C3900g;
import com.goodving.guli.bt.profile.scale.C3907e;
import com.goodving.guli.bt.profile.scale.C3942j;
import com.goodving.guli.bt.profile.scale.FilterBleDevInfo;
import com.goodving.guli.bt.profile.scale.HwConnStatus;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.profile.scale.XMBleMessageHandler;
import com.goodving.guli.bt.profile.scale.XMLeScanCenter;
import com.goodving.guli.bt.profile.scale.WeightProfile;
import com.goodving.guli.view.C4005t;
import com.goodving.guli.view.C4220a;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/11/25.
 */
public final class BLEService extends Service {
    private static final String f19412A;
    private static final String f19413B = "BLEService";
    private static final String mStrPackagePrefix; // f19414C
    public static final String versionNumber = "1.9.6.20140519";
    public static final String mStrConStatusChanged; // f19416b
    public static final String mStrWeightConStatusChanged; //f19417c
    public static final String mStrWeightLowBat; // f19418d
    public static final String mStrSyncDataStatusChanged; // f19419e
    public static final String mStrDynamicStatusChanged; // f19420f
    public static final String mStrDevStatusChanged; // f19421g
    public static final String mStrBatStatusChanged; // f19422h
    public static final String mStrExtraDev; // f19423i
    public static final String mStrExtraParam; // f19424j
    public static final String mStrExtraParamExt; // f19425k
    public static final String mBleMacAddr;
    public static final String mBleUserInfo;
    public static final int f19428n = 3;
    public static final int f19429o = 8;
    public static final int f19430p = 9;
    public static final int f19431q = 10;
    public static final int f19432r = 11;
    public static final int f19433s = 14;
    public static final int f19434t = 15;
    public static final int f19435u = 16;
    public static final int f19436v = 20;
    public static final int f19437w = 21;
    private static final int f19438x = 30000;
    private static final String packageName;
    private static final String TAG;
    private final HandlerThread mHandlerThread;
    private final Handler mHandler;
    private C3942j f19443F;
    private WeightProfile mWeightBleGattCB;
    private final BluetoothAdapter mBleAdapter;
    private BroadcastReceiver mBcReceiver;
    private final Binder mBinder;
    private HwConnStatus mHwConnStatus;
    private volatile boolean f19449L;
    private final Object mObjectA;
    private final Object mObjectB;
    private String mMacInfo;  //f19452O
    private UserInfo mUserInfo;
    private XMLeScanCenter mLeScanCenter;
    private FilterBleDevInfo f19455R;
    private C3907e f19456S;
    private final XMBleMessageHandler mBleMsgHandler;

    static {
        GuliLog.INFO(BLEService.class.getSimpleName() + ".VERSION: " + versionNumber);
        packageName = BLEService.class.getPackage().getName();
        TAG = BLEService.class.getSimpleName();
        f19412A = TAG;
        mStrPackagePrefix = packageName + "." + TAG;
        mStrConStatusChanged = mStrPackagePrefix + ".INTENT_ACTION_CONN_STATUS_CHANGED";
        mStrWeightConStatusChanged = mStrPackagePrefix + ".INTENT_ACTION_WEIGHT_CONN_STATUS_CHANGED";
        mStrWeightLowBat = mStrPackagePrefix + ".INTENT_ACTION_WEIGHT_LOW_BATTERY";
        mStrSyncDataStatusChanged = mStrPackagePrefix + ".INTENT_ACTION_SYNC_DATA_STATUS_CHANGED";
        mStrDynamicStatusChanged = mStrPackagePrefix + ".INTENT_ACTION_DYNAMIC_STATUS_CHANGED";
        mStrDevStatusChanged = mStrPackagePrefix + ".INTENT_ACTION_DEVICE_STATUS_CHANGED";
        mStrBatStatusChanged = mStrPackagePrefix + ".INTENT_ACTION_BATTERY_STATUS_CHANGED";
        mStrExtraDev = mStrPackagePrefix + ".INTENT_EXTRA_DEVICE";
        mStrExtraParam = mStrPackagePrefix + ".INTENT_EXTRA_PARAM";
        mStrExtraParamExt = mStrPackagePrefix + ".INTENT_EXTRA_PARAM_EXT";
        mBleMacAddr = mStrPackagePrefix + ".INTENT_EXTRA_PARAM_ADDRESS";
        mBleUserInfo = mStrPackagePrefix + ".INTENT_EXTRA_PARAM_USERINFO";
    }

    class C3846c extends XMBleMessageHandler {
        final /* synthetic */ BLEService f19461a;

        C3846c(BLEService bLEService) {
            this.f19461a = bLEService;
        }

        public void m25168a(int i) {
        }

        public void m25169a(BluetoothDevice bluetoothDevice, int i) {
            this.f19461a.m25126c(i);
        }

        public void m25170a(BluetoothDevice bluetoothDevice, int i, int i2) {
            this.f19461a.m25109a(i, i2);
        }
    }

    class C3853h implements Thread.UncaughtExceptionHandler {
        final /* synthetic */ BLEService f19468a;

        C3853h(BLEService bLEService) {
            this.f19468a = bLEService;
        }

        public void uncaughtException(Thread thread, Throwable th) {
            GuliLog.m12371e();
            th.printStackTrace();
        }
    }

    class C3854i extends Handler {
        final /* synthetic */ BLEService f19469a;

        C3854i(BLEService bLEService, Looper looper) {
            super(looper);
            this.f19469a = bLEService;

        }

        public void handleMessage(Message message) {
            this.f19469a.m25113a(message);
        }
    }

    public final class C3864q extends Binder implements C3863r {
        private final BLEService mBLEService;

        C3864q(BLEService bLEService) {
            this.mBLEService = bLEService;
        }

        public HwConnStatus m25207a() {
            return this.mBLEService.m25147i();
        }

        public void m25208a(BluetoothDevice bluetoothDevice, boolean z) {
            this.mBLEService.m25140b(bluetoothDevice, z);
        }

        public void m25209a(BluetoothDevice bluetoothDevice, boolean z, UserInfo userInfo) {
            this.mBLEService.m25137a(userInfo);
            if (bluetoothDevice != null) {
                this.mBLEService.m25135a(bluetoothDevice, z);
            } else {
                this.mBLEService.m25133a();
            }
        }

        public void m25210a(C0638c c0638c, long j) {
            this.mBLEService.m25136a(c0638c, j);
        }

        public C3942j m25211b() {
            return this.mBLEService.m25146h();
        }

        public boolean m25212c() {
            return this.mBLEService.m25145g();
        }

        public WeightProfile m25213d() {
            return this.mBLEService.m25141c();
        }

        public void m25214e() {
            this.mBLEService.m25142d();
        }

        public void m25215f() {
            this.mBLEService.m25139b();
        }

        public void m25216g() {
            this.mBLEService.m25134a(39);
        }
    }

    public BLEService() {
        this.f19443F = null;
        this.mWeightBleGattCB = null;
        this.mBleAdapter = BluetoothAdapter.getDefaultAdapter();
        this.mBcReceiver = null;
        this.mBinder = new C3864q(this);
        this.mHwConnStatus = new HwConnStatus();
        this.f19449L = false;
        this.mObjectA = new Object();
        this.mObjectB = new Object();
        this.mMacInfo = null;
        this.mUserInfo = null;
        this.mLeScanCenter = XMLeScanCenter.getInstance();
        this.f19455R = null;
        this.f19456S = null;
        this.mBleMsgHandler = new C3846c(this);
        GuliLog.m12371e();
        this.mHandlerThread = new HandlerThread(f19412A);
        this.mHandlerThread.setUncaughtExceptionHandler(new C3853h(this));
        this.mHandlerThread.start();
        this.mHandler = new C3854i(this, this.mHandlerThread.getLooper());
    }

    private void m25109a(int i, int i2) {
        GuliLog.tracePrintInfo(f19413B, "notifyBatteryStatusChanged:" + i + ",level=" + i2);
        Intent intent = new Intent(mStrBatStatusChanged);
        intent.putExtra(mStrExtraParam, new HwBatteryStatus(i, i2));
        sendBroadcast(intent);
    }

    private void m25110a(int i, BluetoothDevice bluetoothDevice) {
        GuliLog.m12370d(f19413B, "notifyWeightStatusChanged:" + i);
        Intent intent = new Intent(mStrWeightConStatusChanged);
        intent.putExtra(mStrExtraParam, i);
        intent.putExtra(mStrExtraDev, bluetoothDevice);
        sendBroadcast(intent);
    }

    private void m25111a(BluetoothDevice bluetoothDevice, int i) {
        switch (i) {
            case C4220a.f20886i /*0*/:
                GuliLog.m12350a(bluetoothDevice.getAddress() + "[DISCONNECTED]");
            case 1 /*1*/:
                GuliLog.m12350a(bluetoothDevice.getAddress() + "[CONNECTING]");
            case C4220a.f20888k /*2*/:
                GuliLog.m12350a(bluetoothDevice.getAddress() + "[CONNECTED]");
            case f19428n /*3*/:
                GuliLog.m12350a(bluetoothDevice.getAddress() + "[DISCONNECTING]");
            default:
        }
    }

    private void m25112a(Context context, Intent intent) {
        GuliLog.m12371e();
        GuliLog.ASSERTION_RUN_ON_THREAD(this.mHandlerThread);
        String action = intent.getAction();
        GuliLog.m12350a("#Broadcast# Intent: " + intent.getAction());
        if (action.equals("android.intent.action.USER_PRESENT")) {
            GuliLog.INFO("#Broadcast# USER_PRESENT ");
            if (!m25138a(getApplicationContext())) {
                m25134a(39);
            }
        } else if (action.equals("android.bluetooth.adapter.action.STATE_CHANGED")) {
            switch (intent.getIntExtra("android.bluetooth.adapter.extra.STATE", Integer.MIN_VALUE)) {
                case f19431q /*10*/:
                    GuliLog.INFO("#Broadcast# Bluetooth OFF.");
                    this.mHandler.sendEmptyMessage(f19431q);
                case f19432r /*11*/:
                    GuliLog.INFO("#Broadcast# Bluetooth turning ON.");
                    this.mHandler.sendEmptyMessage(f19430p);
                case C4220a.be /*12*/:
                    GuliLog.INFO("#Broadcast# Bluetooth ON.");
                    this.mHandler.sendEmptyMessage(f19429o);
                case C4005t.WeightView_wv_bmi_size /*13*/:
                    GuliLog.INFO("#Broadcast# Bluetooth turning OFF.");
                    this.mHandler.sendEmptyMessage(f19432r);
                default:
                    GuliLog.WARN(">>> UNEXPECTED <<<");
            }
        } else if (action.equals("android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED")) {
//            r1 = intent.getIntExtra("android.bluetooth.adapter.extra.CONNECTION_STATE", -1);
//            GuliLog.m12350a("#Broadcast# state: " + r1 + ", prev state: " + intent.getIntExtra("android.bluetooth.adapter.extra.PREVIOUS_CONNECTION_STATE", -1) + ", device: " + ((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")).getAddress());
        } else if (action.equals("android.bluetooth.device.action.ACL_CONNECTED")) {
            GuliLog.m12350a("#Broadcast# device: " + ((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")).getAddress());
        } else if (action.equals("android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED")) {
            GuliLog.m12350a("#Broadcast# device: " + ((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")).getAddress());
        } else if (action.equals("android.bluetooth.device.action.ACL_DISCONNECTED")) {
            GuliLog.m12350a("#Broadcast# device: " + ((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")).getAddress());
        } else if (action.equals("android.bluetooth.device.action.BOND_STATE_CHANGED")) {
//            r0 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
//            r1 = intent.getIntExtra("android.bluetooth.device.extra.BOND_STATE", -1);
//            GuliLog.m12350a("#Broadcast# bond state: " + r1 + ", prev bond state: " + intent.getIntExtra("android.bluetooth.device.extra.PREVIOUS_BOND_STATE", -1) + ", device: " + r0.getAddress());
        } else if (action.equals("android.bluetooth.device.action.CLASS_CHANGED")) {
            GuliLog.m12350a("#Broadcast# device: " + ((BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE")).getAddress() + ", btClass: " + ((BluetoothClass) intent.getParcelableExtra("android.bluetooth.device.extra.CLASS")).getDeviceClass());
        } else if (action.equals("android.bluetooth.device.action.NAME_CHANGED")) {
//            r0 = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
//            GuliLog.m12350a("#Broadcast# device: " + r0.getAddress() + ", name: " + intent.getStringExtra("android.bluetooth.device.extra.NAME"));
        } else if (!action.equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
        }
    }

    private void m25113a(Message message) {
        GuliLog.m12371e();
        GuliLog.ASSERTION_RUN_ON_THREAD(this.mHandlerThread);
        switch (message.what) {
            case f19428n /*3*/:
            case f19430p /*9*/:
            case f19432r /*11*/:
            case f19433s /*14*/:
            case f19434t /*15*/:
            case f19435u /*16*/:
            case f19429o /*8*/:
                m25130l();
                m25133a();
                break;
            case f19431q /*10*/:
                m25139b();
                m25144f();
                break;
            case f19436v /*20*/:
                m25142d();
                break;
            case f19437w /*21*/:
                m25129k();
                break;
            default:
                GuliLog.WARN(">>> UNEXPECTED <<<");
                break;
        }
    }

    class C3857l implements C1798h {
        final /* synthetic */ String f19474a;
        final /* synthetic */ BLEService f19475b;

        C3857l(BLEService bLEService, String str) {
            this.f19475b = bLEService;
            this.f19474a = str;
        }

        public void m13593a(FilterBleDevInfo filterBleDevInfo) {
        }

        public void onScanedDevice(C3907e c3907e, FilterBleDevInfo filterBleDevInfo) {
            this.f19475b.f19456S = c3907e;
            this.f19475b.m25128j();
        }

        public void m13595b(FilterBleDevInfo filterBleDevInfo) {
            BluetoothDevice a = this.f19475b.f19456S == null ? this.f19475b.m25120b(this.f19474a) : this.f19475b.f19456S.btDev;
            if (a != null) {
                this.f19475b.m25135a(a, true);
            } else {
                this.f19475b.m25121b(1);
            }
        }
    }

    private void m25119a(String str) {
        m25121b(0);
        this.f19456S = null;
        if (this.f19455R == null) {
            // set up the new filter device information.
            this.f19455R = new C3900g().setTimeout((int) f19438x).m25389a(str).m25388a(new C3857l(this, str)).create();
        }

        // start to scan
        this.mLeScanCenter.startScan((Context) this, this.f19455R);
    }

    private BluetoothDevice m25120b(String str) {
        for (BluetoothDevice bluetoothDevice : ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getConnectedDevices(7)) {
            GuliLog.tracePrintInfo(f19413B, "conneted device: " + bluetoothDevice.getName() + "," + bluetoothDevice.getAddress());
            if (bluetoothDevice.getAddress().equalsIgnoreCase(str)) {
                return bluetoothDevice;
            }
        }
        return null;
    }

    private void m25121b(int i) {
        GuliLog.m12370d(f19413B, "notifyStatusChanged:" + i);
        Parcelable hwConnStatus = new HwConnStatus(i);
        this.mHwConnStatus = (HwConnStatus) hwConnStatus;
        Intent intent = new Intent(mStrConStatusChanged);
        intent.putExtra(mStrExtraParam, hwConnStatus);
        sendBroadcast(intent);
    }

    public static void m25122b(Context context) {
        if (context == null) {
            throw new IllegalArgumentException();
        }
        GuliLog.tracePrintInfo(f19413B, "notifyDynamicStatusChanged");
        context.sendBroadcast(new Intent(mStrDynamicStatusChanged));
    }

    private void m25126c(int i) {
        GuliLog.tracePrintInfo(f19413B, "notifyDeviceStatusChanged:" + i);
        Intent intent = new Intent(mStrDevStatusChanged);
        intent.putExtra(mStrExtraParam, i);
        sendBroadcast(intent);
    }

    private void m25128j() {
        if (this.f19455R != null) {
            this.mLeScanCenter.m25373a(this.f19455R);
            this.f19455R = null;
        }
    }

    private void m25129k() {
        if (this.mUserInfo == null) {
            GuliLog.m12370d(f19413B, "user info is null!!!");
            m25121b(4);
            return;
        }
        GuliLog.tracePrintInfo(f19413B, "userinfo:" + this.mUserInfo);
//TBD LOOKS like is for ring        new C3812C(this.mUserInfo, new C3860n(this), new C3861o(this)).m9824f();
    }

    private void m25130l() {
        synchronized (this.mObjectA) {
            this.f19449L = false;
        }
    }

    private void m25131m() {
        m25132n();
    }

    private void m25132n() {
        sendBroadcast(new Intent(mStrWeightLowBat));
    }

    // on start to execute this method
    public synchronized void m25133a() {
        GuliLog.tracePrintInfo(f19413B, "in connectDevice");
        if (this.mMacInfo != null && BluetoothAdapter.checkBluetoothAddress(this.mMacInfo)) {
            String str = this.mMacInfo;
            if (this.f19443F != null) {
                GuliLog.tracePrintInfo(f19413B, "miliProfile != null, miliProfile.getState() = " + this.f19443F.m25279g());
                BluetoothDevice f = this.f19443F.m25277f();
                if (f != null) {
                    m25111a(f, ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getConnectionState(f, BluetoothProfile.GATT));
                    if (f.getAddress().equals(str)) {
                        GuliLog.tracePrintInfo(f19413B, "miliProfile has device:" + f.getName() + "," + f.getAddress());
                    }
                }
                this.f19443F.m25294a(false);
                // make sure there is no connection right now, to disconnect if there is any.
                this.f19443F.m25267b();
                this.f19443F = null;
            }

            // to check if already has this device connected, if yes, get this device instance, otherwise return null
            BluetoothDevice b = m25120b(str);
            if (b != null) {
                // start connecting to the gatt
                m25135a(b, true);
            } else {
                b = this.mBleAdapter.getRemoteDevice(str);
                GuliLog.tracePrintInfo(f19413B, "mili device type:" + b.getType());
                if (b.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                    // if it is the LE device.
                    // start connecting to the gatt
                    m25135a(b, true);
                } else {
                    // start to scan
                    m25119a(str);
                }
            }
        }
    }

    class C3852g extends C0638c {
        final /* synthetic */ int f19465a;
        final /* synthetic */ PowerManager.WakeLock f19466b;
        final /* synthetic */ BLEService f19467d;

        C3852g(BLEService bLEService, int i, PowerManager.WakeLock wakeLock) {
            this.f19467d = bLEService;
            this.f19465a = i;
            this.f19466b = wakeLock;
        }

        public void m25175a() {
            super.m7910a();
        }

        public void m25176a(Object obj) {
            super.m7912a(obj);
            if (((Integer) obj).intValue() == 8) {
                GuliLog.m12360b("BLEService", "setLatency success......" + this.f19465a);
            } else {
                GuliLog.m12360b("BLEService", "setLatency failed......" + this.f19465a);
            }
            GuliLog.m12360b("BLEService", "waklock release......" + this.f19465a);
            if (this.f19466b.isHeld()) {
                this.f19466b.release();
            }
        }

        public void m25177b(Object obj) {
            super.m7914b(obj);
            GuliLog.m12360b("BLEService", "setLatency failed......" + this.f19465a);
            GuliLog.m12360b("BLEService", "waklock release......" + this.f19465a);
            if (this.f19466b.isHeld()) {
                this.f19466b.release();
            }
        }
    }

    public void m25134a(int i) {
        GuliLog.m12360b(f19413B, "current latency level set level:" + i);
        PowerManager.WakeLock newWakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(1, "setLatency");
        GuliLog.m12360b(f19413B, "waklock acquire......" + i);
        newWakeLock.acquire();
        new C3831s(i, new C3852g(this, i, newWakeLock)).m9826h();
    }

    class C3858m implements C1724A {
        final /* synthetic */ BLEService f19476a;

        C3858m(BLEService bLEService) {
            this.f19476a = bLEService;
        }

        public void onDeviceConnected(BluetoothDevice bluetoothDevice) {
            GuliLog.m12370d("BLEService", "onDeviceConnected................");
        }

        public void onDeviceConnectionFailed(BluetoothDevice bluetoothDevice) {
            GuliLog.m12370d("BLEService", "onDeviceConnectionFailed................");
            this.f19476a.m25121b(4);
        }

        public void onDeviceDisconnected(BluetoothDevice bluetoothDevice) {
            GuliLog.m12370d("BLEService", "onDeviceDisconnected................");
            this.f19476a.m25121b(5);
        }

        public void onInitializationFailed(BluetoothDevice bluetoothDevice) {
            GuliLog.m12370d("BLEService", "onInitializationFailed................");
            this.f19476a.m25121b(4);
        }

        public void onInitializationSuccess(BluetoothDevice bluetoothDevice) {
            GuliLog.m12370d("BLEService", "onInitializationSuccess................");
            this.f19476a.mHandler.sendEmptyMessage(21);
        }
    }


    public void m25135a(BluetoothDevice bluetoothDevice, boolean z) {
        // send bc to notify the connection status changed.
        m25121b(2);
        this.f19443F = new C3942j(this, bluetoothDevice, new C3858m(this));
        this.f19443F.m25586a(this.mBleMsgHandler);
        // set the call back status
        this.f19443F.m25294a(z);

        // start to connect to the gatt.
        this.f19443F.m25263a();
    }

    public void m25136a(C0638c c0638c, long j) {
        GuliLog.tracePrintInfo(f19413B, "in syncWeightData");
        synchronized (this.mObjectB) {
            if (this.mWeightBleGattCB == null) {
                return;
            }
            this.mWeightBleGattCB.runSyncDataTask(c0638c, j);
            GuliLog.tracePrintInfo(f19413B, "out syncWeightData");
        }
    }

    public void m25137a(UserInfo userInfo) {
        this.mUserInfo = userInfo;
    }

    public boolean m25138a(Context context) {
        List runningTasks = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
        if (!(runningTasks == null || runningTasks.isEmpty())) {
            ComponentName componentName = ((ActivityManager.RunningTaskInfo) runningTasks.get(0)).topActivity;
            GuliLog.m12370d(f19413B, "topActivity:" + componentName.flattenToString());
            if (!componentName.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    public synchronized void m25139b() {
        GuliLog.tracePrintInfo(f19413B, "in disconnectDevice");
        m25128j();
        if (this.f19443F != null) {
            this.f19443F.m25294a(false);
            this.f19443F.m25267b();
            this.f19443F = null;
        }
        this.mHwConnStatus = new HwConnStatus();
    }

    class C3862p implements C1724A {
        final /* synthetic */ BLEService f19479a;

        C3862p(BLEService bLEService) {
            this.f19479a = bLEService;
        }

        public void onDeviceConnected(BluetoothDevice bluetoothDevice) {
            this.f19479a.m25110a(2, bluetoothDevice);
        }

        public void onDeviceConnectionFailed(BluetoothDevice bluetoothDevice) {
            this.f19479a.mHandler.sendEmptyMessage(20);
            this.f19479a.m25110a(1, bluetoothDevice);
        }

        public void onDeviceDisconnected(BluetoothDevice bluetoothDevice) {
            this.f19479a.mHandler.sendEmptyMessage(20);
            this.f19479a.m25110a(0, bluetoothDevice);
        }

        public void onInitializationFailed(BluetoothDevice bluetoothDevice) {
            this.f19479a.mHandler.sendEmptyMessage(20);
            this.f19479a.m25110a(4, bluetoothDevice);
        }

        public void onInitializationSuccess(BluetoothDevice bluetoothDevice) {
            this.f19479a.m25110a(3, bluetoothDevice);
        }
    }

    // scale weight information
    class C3848d implements WeightProfile.C3847c {
        final /* synthetic */ BLEService f19462a;

        C3848d(BLEService bLEService) {
            this.f19462a = bLEService;
        }

        public void m25171a(ScaleWeightInfo c3925e) {
            EventBus.getDefault().post(c3925e);
        }
    }

    // low battery
    class C3851f implements Runnable {
        final /* synthetic */ C3850e f19464a;

        C3851f(C3850e c3850e) {
            this.f19464a = c3850e;
        }

        public void run() {
            this.f19464a.f19463a.m25131m();
        }
    }

    class C3850e implements WeightProfile.C3849b {
        final /* synthetic */ BLEService f19463a;

        C3850e(BLEService bLEService) {
            this.f19463a = bLEService;
        }

        public void m25173a() {
            GuliLog.m12370d("BLEService", "onLowBattery");
            this.f19463a.mHandler.post(new C3851f(this));
        }
    }

    public void m25140b(BluetoothDevice bluetoothDevice, boolean z) {
        GuliLog.m12370d(f19413B, "in connectWeight:" + bluetoothDevice.getName() + "," + bluetoothDevice.getAddress());
        synchronized (this.mObjectB) {
            if (this.mWeightBleGattCB != null) {
                if (this.mWeightBleGattCB.m25277f().getAddress().equals(bluetoothDevice.getAddress())) {
                    GuliLog.m12370d(f19413B, "use current weight profile!!!");
                    return;
                }
                GuliLog.m12370d(f19413B, "disconnect current weight profile!!!");
                this.mWeightBleGattCB.m25294a(false);
                this.mWeightBleGattCB.m25527r();
                this.mWeightBleGattCB.m25267b();
                this.mWeightBleGattCB = null;
            }
            this.mWeightBleGattCB = new WeightProfile(this, bluetoothDevice, new C3862p(this));
            this.mWeightBleGattCB.m25524a(new C3848d(this));
            this.mWeightBleGattCB.m25523a(new C3850e(this));
            this.mWeightBleGattCB.m25294a(z);
            this.mWeightBleGattCB.m25263a();
            GuliLog.m12370d(f19413B, "out connectWeight");
        }
    }

    public WeightProfile m25141c() {
        WeightProfile weightProfile;
        GuliLog.tracePrintInfo(f19413B, "in getWeigthProfile:" + this.mWeightBleGattCB);
        synchronized (this.mObjectB) {
            weightProfile = this.mWeightBleGattCB;
        }
        return weightProfile;
    }

    public void m25142d() {
        GuliLog.tracePrintInfo(f19413B, "in disconnectWeight");
        synchronized (this.mObjectB) {
            if (this.mWeightBleGattCB != null) {
                this.mWeightBleGattCB.m25294a(false);
                this.mWeightBleGattCB.m25267b();
                this.mWeightBleGattCB = null;
            }
        }
        GuliLog.tracePrintInfo(f19413B, "out disconnectWeight");
    }

    public void m25143e() {
        synchronized (this.mObjectA) {
            if (!this.f19449L) {
                this.f19449L = true;
                GuliLog.tracePrintInfo(f19413B, "disable BluetoothAdapter");
                BluetoothAdapter.getDefaultAdapter().disable();
            }
        }
    }

    public void m25144f() {
        synchronized (this.mObjectA) {
            if (this.f19449L) {
                GuliLog.tracePrintInfo(f19413B, "enable BluetoothAdapter");
                BluetoothAdapter.getDefaultAdapter().enable();
            }
        }
    }

    public boolean m25145g() {
        boolean z;
        synchronized (this.mObjectA) {
            z = this.f19449L;
        }
        return z;
    }

    public synchronized C3942j m25146h() {
        return this.f19443F;
    }

    public HwConnStatus m25147i() {
        return this.mHwConnStatus;
    }

    public IBinder onBind(Intent intent) {
        GuliLog.m12371e();
        return this.mBinder;
    }

    public void onConfigurationChanged(Configuration configuration) {
        GuliLog.m12371e();
        super.onConfigurationChanged(configuration);
    }

    class C3856k implements Runnable {
        final /* synthetic */ Context f19471a;
        final /* synthetic */ Intent f19472b;
        final /* synthetic */ C3855j f19473c;

        C3856k(C3855j c3855j, Context context, Intent intent) {
            this.f19473c = c3855j;
            this.f19471a = context;
            this.f19472b = intent;
        }

        public void run() {
            this.f19473c.f19470a.m25112a(this.f19471a, this.f19472b);
        }
    }

    class C3855j extends BroadcastReceiver {
        final /* synthetic */ BLEService f19470a;

        C3855j(BLEService bLEService) {
            this.f19470a = bLEService;
        }

        public void onReceive(Context context, Intent intent) {
            this.f19470a.mHandler.post(new C3856k(this, context, intent));
        }
    }

    public void onCreate() {
        GuliLog.m12371e();
        GuliLog.m12370d(f19413B, "onCreate");
        super.onCreate();
        this.mBcReceiver = new C3855j(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        intentFilter.addAction("android.intent.action.SCREEN_ON");
        intentFilter.addAction("android.intent.action.USER_PRESENT");
        registerReceiver(this.mBcReceiver, intentFilter);
    }

    public void onDestroy() {
        GuliLog.m12371e();
        GuliLog.m12370d(f19413B, "onDestroy");
        m25139b();
        unregisterReceiver(this.mBcReceiver);
        super.onDestroy();
    }

    public void onLowMemory() {
        GuliLog.m12371e();
        super.onLowMemory();
    }

    public void onRebind(Intent intent) {
        GuliLog.m12371e();
        GuliLog.WARN(">>> UNEXPECTED <<<");
        super.onRebind(intent);
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        GuliLog.m12370d(f19413B, "onStartCommand Intent: " + intent);
        if (intent != null) {
            this.mMacInfo = intent.getStringExtra(mBleMacAddr);
            this.mUserInfo = (UserInfo) intent.getParcelableExtra(mBleUserInfo);
        }
        if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
            m25133a();
        }

        return START_REDELIVER_INTENT;
    }

    public void onTaskRemoved(Intent intent) {
        GuliLog.m12371e();
        GuliLog.WARN("Calling onDestroy()...");
        super.onTaskRemoved(intent);
    }

    public void onTrimMemory(int i) {
        GuliLog.m12371e();
        super.onTrimMemory(i);
    }

    public boolean onUnbind(Intent intent) {
        GuliLog.m12371e();
        return super.onUnbind(intent);
    }
}

